package com.vf.admin.fiwork.controller;

import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppTaskRecordService;
import com.vf.admin.client.service.AppTaskService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;

/**
 * 日常任务
 */
@Slf4j
@Controller
public class AppDailyTasController extends BaseController {

    @Autowired
    private AppTaskService appTaskService;
    @Autowired
    private AppTaskRecordService appTaskRecordService;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 完成新手任务
     *
     * @param userId
     * @param typeNameEn
     */
    public void getAwardOnce(Integer userId, String typeNameEn) {
        //当前任务是否已完成
        Boolean complete = appTaskRecordService.isCompleteOnce(userId, typeNameEn);
        if (!complete) {
            AppTask task = appTaskService.infoByTypeNameEn(typeNameEn);
            appTaskRecordService.addTaskRecord(userId, typeNameEn);
            //增加钻石
            appUserService.addDiamonds(userId, new BigDecimal(String.valueOf(task.getDiamonds())), "任务赠送");
        }
    }

    /**
     * 完成新手任务
     * 给3条动态评论
     *
     * @param userId
     * @param typeNameEn
     */
    public void getAwardDynamicCommentOnce(Integer userId, String typeNameEn) {
        //当前任务是否已完成
        Boolean complete = appTaskRecordService.isCompleteOnce(userId, typeNameEn);
        if (!complete) {
            int count = 1;
            Object object = redisUtil.get(RedisKeys.TASK_ONCE_COMMENT + userId);
            if (object != null) {
                count = Integer.valueOf(String.valueOf(object)) + 1;
            }

            //满足三次评论之后发放奖励
            if (count == 3) {
                AppTask task = appTaskService.infoByTypeNameEn(typeNameEn);
                appTaskRecordService.addTaskRecord(userId, typeNameEn);
                //增加钻石
                appUserService.addDiamonds(userId, new BigDecimal(String.valueOf(task.getDiamonds())), "任务赠送");

                redisUtil.del(RedisKeys.TASK_ONCE_COMMENT + userId);
            } else {
                redisUtil.set(RedisKeys.TASK_ONCE_COMMENT + userId, count);
            }
        }
    }

    /**
     * 完成新手任务
     * 关注3个人
     * @param userId
     * @param typeNameEn
     */
    public void getAwardFocusOnce(Integer userId, String typeNameEn) {
        //当前任务是否已完成
        Boolean complete = appTaskRecordService.isCompleteOnce(userId, typeNameEn);
        if (!complete) {
            int count = 1;
            Object object = redisUtil.get(RedisKeys.TASK_ONCE_FOCUS + userId);
            if (object != null) {
                count = Integer.valueOf(String.valueOf(object)) + 1;
            }

            //满足三次评论之后发放奖励
            if (count == 3) {
                AppTask task = appTaskService.infoByTypeNameEn(typeNameEn);
                appTaskRecordService.addTaskRecord(userId, typeNameEn);
                //增加钻石
                appUserService.addDiamonds(userId, new BigDecimal(String.valueOf(task.getDiamonds())), "任务赠送");

                redisUtil.del(RedisKeys.TASK_ONCE_FOCUS + userId);
            } else {
                redisUtil.set(RedisKeys.TASK_ONCE_FOCUS + userId, count);
            }
        }
    }

    /**
     * 完成每日任务
     * @param userId
     * @param typeNameEn
     */
    public void getAwardEveryDay(Integer userId, String typeNameEn) {
        //今日任务是否已完成
        Boolean complete = appTaskRecordService.isCompleteToday(userId, typeNameEn);
        if (!complete) {
            AppTask task = appTaskService.infoByTypeNameEn(typeNameEn);
            appTaskRecordService.addTaskRecord(userId, typeNameEn);
            //增加钻石
            appUserService.addDiamonds(userId, new BigDecimal(String.valueOf(task.getDiamonds())), "任务赠送");
        }
    }
}
